/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.bean.parametrics;

import com.giant.entities.InLine;
import com.giant.entities.InLinePK;
import com.giant.exceptions.OperacionInvalidaException;
import com.giant.services.parametrics.ServiceLineLocal;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author taticay
 */
@ManagedBean
@SessionScoped
public class LineBean implements Serializable{

    
       /**
     * Relación con la interfaz que provee los servicios necesarios del vendedor
     */
    @EJB
    private ServiceLineLocal serviceLine;
    
    private String param="";
    
    private List<InLine> allLines;
    
      //-----------------------------------------------------------
    // Atributos
    //-----------------------------------------------------------

    /**
     * Representa un nuevo vendedor a ingresar
     */
    private InLine line = new InLine();
    
   
     
     //-----------------------------------------------------------
    // Getters y setters
    //-----------------------------------------------------------

   public LineBean() {
    }
    /**
     * Devuelve el objeto de vendedor actual
     * @return vendedor Vendedor actual
     */
    public InLine getLine()
    {
        return line;
    }

    /**
     * Modifica al vendedor actual
     * @param vendedor Nuevo vendedor
     */
    public void setLine(InLine line)
    {
        this.line = line;
    }

    /**
     * Devuelve todos los vendedores del sistema
     * @return vendedores Lista con todos los vendedores del sistema
     */
    public List<InLine> getLines()
    {
        if(allLines==null)
        {
            allLines=serviceLine.getLine();
        }
        return allLines;
    }
    
   
    
      public void handleLineChange( String sql, String param) {  
             System.out.println("entro");
          System.out.println(this.param);
          Map<String,String> map= new HashMap<String, String>();  
          map.put(param, this.param);
          
         allLines=this.serviceLine.getLineFilter(sql, map);
         
    } 
      

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }
      
    public void onRowSelect(SelectEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Product Selected", ((InLine) event.getObject()).getInLinePK()+ ""));
    }

    public void onRowUnselect(UnselectEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Product UnSelected", ((InLine) event.getObject()).getInLinePK() + ""));
    }
    public boolean saveLine() {
        boolean ret = true;
        try {
            System.out.println("datos:"+line.getCharacters()+"-"+line.getDescription()+"-"+line.getInBrand().getIdBrand());
            InLinePK linePK=new InLinePK();
            linePK.setIdBrand(line.getInBrand().getIdBrand());
            line.setInLinePK(linePK);
            serviceLine.agregarLine(line);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Successful", "Successful save!"));
            line = new InLine();

        } catch (OperacionInvalidaException ex) {
            Logger.getLogger(LineBean.class.getName()).log(Level.SEVERE, null, ex);
            ret = false;
        }

        return ret;
    }
    public void resetFail() {
        line = new InLine();
        RequestContext.getCurrentInstance().reset("j_idt20:form:editPanel");
    }
   
}
